﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        MainWindow main;

        public Options()
        {
            InitializeComponent();

            // Adds appropriate video resolutions to the dropdown list
            resolutionBox.Items.Add(ColorImageFormat.RgbResolution640x480Fps30);
            resolutionBox.Items.Add(ColorImageFormat.RgbResolution1280x960Fps12);
            resolutionBox.SelectedIndex = 0;
            kinectSelectBox.Items.Add(MainWindow.KinectNumber.ONE);
            kinectSelectBox.Items.Add(MainWindow.KinectNumber.TWO);
            kinectSelectBox.SelectedIndex = 0;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Arrange the window in the corner to maximize space
            this.Left = 0;
            this.Top = 0;
            
            // Create the main window
            main = new MainWindow((int)numberSlider.Maximum, (ColorImageFormat)(resolutionBox.SelectedItem),
                (int)numberSlider.Value, (int)sizeSlider.Value, Math.Abs((int)updateSlider.Value), 
                int.Parse(near.Text), int.Parse(far.Text));
            
            // Arrange the main window so that it does not start overlaped with the options windo
            main.Left = this.Left + Width;
            main.Top = this.Top;
            main.Show();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (main != null)
                main.Close();
        }

        //Methods to pass values to main window
        #region Slider Values Changed
        private void sizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (main != null)
            {
                main.spotSize = (int)(sizeSlider.Value);
                SizeLabel.Content = "Size of Spots: " + (int)(sizeSlider.Value);
            }
        }

         private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (main != null)
            {
                main.resolution = (ColorImageFormat)(resolutionBox.SelectedItem);
            }
        }

        private void numberSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (main != null)
            {
                main.totalSpots = (int)numberSlider.Value;
                NumberLabel.Content = "Number of Spots: " + (int)numberSlider.Value;
            }
        }

        private void updateSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (main != null)
            {
                main.updateSpeed = Math.Abs((int)updateSlider.Value);
                UpdateLabel.Content = "Update Speed: 1/" + Math.Abs((int)updateSlider.Value);
            }
        }
        #endregion Slider Values Changed

        private void kinectSelectBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (main != null) {
                main.kNumber = (MainWindow.KinectNumber)(kinectSelectBox.SelectedItem);
            }
        }

        private void near_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (int.Parse(near.Text) < 1) near.Text = "-9";
            if (main != null)
            {
                main.near = int.Parse(near.Text);
            }
        }

        private void far_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (main != null)
            {
                main.far = int.Parse(far.Text);
            }
        }

        private void fullRange_Click(object sender, RoutedEventArgs e)
        {
            near.Text = "0";
            far.Text = "50000";
        }

    }
}
